/***************************************************************************
 * Copyright (C) 2011-2013
 * by Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * ProgressThrobber.cpp
 *
 * for SaveGame Manager GX
 ***************************************************************************/
#include "ProgressThrobber.hpp"
#include "../Themes/CTheme.h"

/****************************************************************************
* 
* ProgressThrobber Class
*
***************************************************************************/
ProgressThrobber::ProgressThrobber()
	: GuiFrame(0, 0)
{
	timer.reset();
	
	count = 1;
	
	throbberBg = Resources::GetImageData("manage_throbber_bg.png");
	throbberBgImg = new GuiImage(throbberBg);
	
	SetSize(throbberBgImg->GetWidth(), throbberBgImg->GetHeight());
	
	throbber = Resources::GetImageData("manage_throbber.png");
	
	throbberCentreImg = new GuiImage(throbber);
	throbberCentreImg->SetAlignment(ALIGN_CENTER | ALIGN_MIDDLE);
	
	throbberLT_Img = new GuiImage(throbber);
	throbberLT_Img->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	throbberLT_Img->SetPosition(4, 4);
	throbberLT_Img->SetVisible(false);
	
	throbberCT_Img = new GuiImage(throbber);
	throbberCT_Img->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	throbberCT_Img->SetPosition(16, 4);
	
	throbberRT_Img = new GuiImage(throbber);
	throbberRT_Img->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	throbberRT_Img->SetPosition(28, 4);
	
	throbberRM_Img = new GuiImage(throbber);
	throbberRM_Img->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	throbberRM_Img->SetPosition(28, 16);
	
	throbberRB_Img = new GuiImage(throbber);
	throbberRB_Img->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	throbberRB_Img->SetPosition(28, 28);
	
	throbberCB_Img = new GuiImage(throbber);
	throbberCB_Img->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	throbberCB_Img->SetPosition(16, 28);
	
	throbberLB_Img = new GuiImage (throbber);
	throbberLB_Img->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	throbberLB_Img->SetPosition(4, 28);
	
	throbberLM_Img = new GuiImage(throbber);
	throbberLM_Img->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	throbberLM_Img->SetPosition(4, 16);
	
	Append(throbberBgImg);
	Append(throbberCentreImg);
	Append(throbberLT_Img);
	Append(throbberCT_Img);
	Append(throbberRT_Img);
	Append(throbberRM_Img);
	Append(throbberRB_Img);
	Append(throbberCB_Img);
	Append(throbberLB_Img);
	Append(throbberLM_Img);
}

ProgressThrobber::~ProgressThrobber()
{
	RemoveAll();

	delete throbberBgImg;
	delete throbberCentreImg;
	delete throbberLT_Img;
	delete throbberCT_Img;
	delete throbberRT_Img;
	delete throbberRM_Img;
	delete throbberRB_Img;
	delete throbberCB_Img;
	delete throbberLB_Img;
	delete throbberLM_Img;
	
	Resources::Remove(throbber);
	Resources::Remove(throbberBg);
}

void ProgressThrobber::Draw()
{
	if(timer.elapsed() > 0.15)
	{
		timer.reset();
		
		count++;
		if(count > 8)
			count = 1;
		
		switch(count)
		{
			case 1:
				throbberLT_Img->SetVisible(false);
				throbberLM_Img->SetVisible(true);
				break;
			case 2:
				throbberCT_Img->SetVisible(false);
				throbberLT_Img->SetVisible(true);
				break;
			case 3:
				throbberRT_Img->SetVisible(false);
				throbberCT_Img->SetVisible(true);
				break;
			case 4:
				throbberRM_Img->SetVisible(false);
				throbberRT_Img->SetVisible(true);
				break;
			case 5:
				throbberRB_Img->SetVisible(false);
				throbberRM_Img->SetVisible(true);
				break;
			case 6:
				throbberCB_Img->SetVisible(false);
				throbberRB_Img->SetVisible(true);
				break;
			case 7:
				throbberLB_Img->SetVisible(false);
				throbberCB_Img->SetVisible(true);
				break;
			case 8:
				throbberLM_Img->SetVisible(false);
				throbberLB_Img->SetVisible(true);
				break;
		}
	}

	GuiFrame::Draw();
}
